package com.txf.simple.ui.activity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.txf.simple.R;
import com.txf.simple.tools.Logger;

public class ViewPagerActivity extends AbsBaseActivity {
    private ViewPager mViewPager;
    private PagerAdapter mPagerAdapter;

    @Override
    protected View buildContentView() {
        return LayoutInflater.from(this).inflate(R.layout.activity_view_pager,null);
    }
    @Override
    protected void initViews() {
        mViewPager = (ViewPager)findViewById(R.id.view_pager);
        initData();
    }
    int[] imageID = new int[]{R.drawable.ic_camera,R.drawable.ic_storage,R.drawable.ic_location,
            R.drawable.ic_camera,R.drawable.ic_storage,R.drawable.ic_location};

    private void initData() {
        mViewPager.setPageMargin(20);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(mPagerAdapter = new PagerAdapter() {
            @Override
            public Object instantiateItem(ViewGroup container, int position)
            {
                int newPosition = position%3;
                Logger.i("View总数 ; "+container.getChildCount());
                Logger.i("位置"+position+" 新位置"+newPosition);
                ImageView view = new ImageView(getContext());
                view.setBackgroundColor(0XFF123456);
                view.setImageResource(imageID[newPosition]);
                container.addView(view);
                return view;
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object)
            {
                container.removeView((View) object);
            }
            @Override
            public int getCount() {
                return Integer.MAX_VALUE;
            }
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });
    }
}
